﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections.Generic;

namespace TRIG_MOD_STATUSSUMMARY.DR_StatusSummary
{
    public partial class DR_StatusSummaryUserControl : UserControl
    {
        public DR_StatusSummary ParentWebPart
        {
            get;
            set;
        }

        DataForStatusSummary objCls = new DataForStatusSummary();
        List<string> docTypes = new List<string>();
        List<string> discipAbb = new List<string>();

        public List<string> DocTypes
        {
            get { return docTypes; }
            set { docTypes = value; }
        }

        public List<string> Disciplines
        {
            get { return discipAbb; }
            set { discipAbb = value; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                stDT.MaxDate = DateTime.Today;
                endDT.MaxDate = DateTime.Today;
                stDT.SelectedDate = DateTime.Today;

            }
            catch (Exception x)
            {

                errorMessage.Text += x.Message;
            }
        }

        protected void rdDates_SelectedIndexChanged2(object sender, EventArgs e)
        {
            stSumGrid.DataSource = null;
            stSumGrid.Visible = false;
            //if (Convert.ToInt32(rdDates.SelectedItem.Value) == 0)
            //    endDT.Visible = true;
            //else

            //    endDT.Visible = true;
        }

        //protected void btnProcess_Click1(object sender, EventArgs e)
        //{
        //    NullLabels();
        //    discipAbb = objCls.GetDiscipiline();
        //    objCls.CreateTable("DR");
        //    if (Convert.ToInt32(rdDates.SelectedItem.Value) == 1)
        //        GetDates();
        //    else
        //        GetDates("Today");
        //    objCls.rdDates = rdDates;
        //    objCls.grid = stSumGrid;

        //    objCls.GetDocItemsbyDiscipiline(ParentWebPart.docTypeName, Disciplines, errorMessage, "DR");
        //    objCls.BindGrid(stSumGrid,"DR");

        //}

        private void NullLabels()
        {
            errorMessage.Text = "";
            stSumGrid.Visible = true;
        }

        private void GetDates()
        {
            string thisMonthStart = Convert.ToString(stDT.SelectedDate.ToShortDateString());
            DateTime tmStDt = Convert.ToDateTime(thisMonthStart);
            thisMonthStart = Convert.ToString(tmStDt.AddDays(1 - tmStDt.Day));
            string thisMonthToday = Convert.ToString(new DateTime(tmStDt.Year, tmStDt.Month, 1).AddMonths(1).AddDays(-1)); //DateTime.Now.ToLongTimeString();
            //var yr = DateTime.Today.Year;
            //var mth = DateTime.Today.Month;
            var yr = stDT.SelectedDate.Year;
            var mth = stDT.SelectedDate.Month;
            var firstDay = new DateTime(yr, mth, 1).AddMonths(-1);
            var lastDay = new DateTime(yr, mth, 1).AddDays(-1);
            string lastMonthStart = Convert.ToString(firstDay);
            string lastMonthEnd = Convert.ToString(lastDay);

            objCls.cpStDT = thisMonthStart;
            objCls.cpEndDT = thisMonthToday;
            objCls.lpStDt = lastMonthStart;
            objCls.lpEndDt = lastMonthEnd;
        }

        private void GetDates(string today)
        {
            string todays = Convert.ToString(stDT.SelectedDate.ToShortDateString());
            string yesterday = Convert.ToString(stDT.SelectedDate.AddDays(-1));
            objCls.cpStDT = todays;
            objCls.cpEndDT = todays;
            objCls.lpStDt = yesterday;
            objCls.lpEndDt = yesterday;
        }

        protected void btnProcessDR_Click(object sender, EventArgs e)
        {
            NullLabels();
            discipAbb = objCls.GetDiscipiline();
            objCls.CreateTable("DR");
            if (Convert.ToInt32(rdDates.SelectedItem.Value) == 1)
                GetDates();
            else
                GetDates("Today");
            objCls.rdDates = rdDates;
            objCls.grid = stSumGrid;

            objCls.GetDocItemsbyDiscipiline(ParentWebPart.docTypeName, Disciplines, errorMessage, "DR");
            objCls.BindGrid(stSumGrid, "DR");
        }

    }
}
